package com.yeyks.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.em.accountinfo.AccountInfoTypeEnum;
import com.yeyks.common.em.mattress.OrderInfoMattressEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.commonReference.service.mattress.HotelMattressOverviewService;
import com.yeyks.commonReference.service.mattress.OrderInfoMattressService;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.mattress.dal.domain.OrderInfoMattress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author shuai
 * @date 2019/10/28 13:28
 */
@Component
@Slf4j
public class OrderMattressCompleteTask {
    @Autowired
    private OrderInfoMattressService mattressService;
    @Autowired
    private HotelMattressOverviewService overviewService;
    @Autowired
    private HotelInfoService hotelInfoService;
    @Autowired
    private AccountInfoService accountInfoService;

    @Scheduled(cron = "${schedule.mattress.complete}")
    private void orderMattressCompleteTask() {
        log.info("床垫订单完成执行，time = {}", DateUtil.formatDateTime(new Date()));
        Date date = new Date();
        List<OrderInfoMattress> orderInfoMattresses = mattressService.list(new QueryWrapper<OrderInfoMattress>()
                .eq(OrderInfoMattress.CALCULATE_FLAG, OrderInfoMattressEnum.CalculateFlag.WAIT.value())
                .eq(OrderInfoMattress.STATUS, OrderInfoMattressEnum.Status.STARTUP_SUCCESSFUL.value())
                .lt(OrderInfoMattress.OVER_TIME, date));
        for (OrderInfoMattress order : orderInfoMattresses) {
            order.setCalculateFlag(OrderInfoMattressEnum.CalculateFlag.COMPLETE.value());
            order.setModifyTime(new Date());
            if (mattressService.updateById(order)) {
                try {
                    //通过酒店id查询酒店对象
                    HotelInfo hotelInfo = hotelInfoService.selectHotelInfoById(order.getHotelId());
                    Arguments.beanIsNull(hotelInfo, "酒店不存在");
                    //给酒店发放佣金
                    overviewService.add(hotelInfo.getOperatorId(), AccountInfoTypeEnum.HotelManager.value(), order.getOrderNo(), order.getSnapshotTotalFee(), order.getHotelFeeValue());
                    //给代理发放佣金
//                    Map<Integer, Integer> map = accountInfoService.selectExcludeParentIdAndTypeById(hotelInfo.getOperatorId());
//                    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
//                        if (1403 == entry.getKey()) {
//                            continue;
//                        }
//                        //计算
//                        Integer amount = accountInfoService.selectByIdAndType(entry.getKey(), entry.getValue(), 2, order.getSnapshotTotalFee());
//                        overviewService.add(entry.getKey(), entry.getValue(), order.getOrderNo(), order.getSnapshotTotalFee(), amount);
//                    }
                } catch (ServiceException e) {
                    log.info(e.getMessage() + "；酒店ID = {}", order.getHotelId());
                    log.info("检查床垫订单是否完成写入账户余额异常orderNo = {}", order.getOrderNo());
                } catch (Exception e) {
                    log.info("检查床垫订单是否完成写入账户余额异常orderNo = {}", order.getOrderNo());
                    e.printStackTrace();
                }
            } else {
                log.info("床垫订单修改状态异常orderNo = {}", order.getOrderNo());
            }
            log.info("床垫订单完成执行end");
        }
    }
}
